/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import concursos.concurso.Inscricao;
import controllers.exceptions.NonexistentEntityException;
import controllers.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import concursos.concurso.Concurso;
import concursos.concurso.Candidato;

/**
 *
 * @author lucena
 */
public class InscricaoJpaController {

    public InscricaoJpaController() {
        emf = Persistence.createEntityManagerFactory("concursosPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Inscricao inscricao) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Concurso concurso = inscricao.getConcurso();
            if (concurso != null) {
                concurso = em.getReference(concurso.getClass(), concurso.getId());
                inscricao.setConcurso(concurso);
            }
            Candidato candidato = inscricao.getCandidato();
            if (candidato != null) {
                candidato = em.getReference(candidato.getClass(), candidato.getId());
                inscricao.setCandidato(candidato);
            }
            em.persist(inscricao);
            if (concurso != null) {
                concurso.getInscricoes().add(inscricao);
                concurso = em.merge(concurso);
            }
            if (candidato != null) {
                Inscricao oldInscricaoOfCandidato = candidato.getInscricao();
                if (oldInscricaoOfCandidato != null) {
                    oldInscricaoOfCandidato.setCandidato(null);
                    oldInscricaoOfCandidato = em.merge(oldInscricaoOfCandidato);
                }
                candidato.setInscricao(inscricao);
                candidato = em.merge(candidato);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findInscricao(inscricao.getId()) != null) {
                throw new PreexistingEntityException("Inscricao " + inscricao + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Inscricao inscricao) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Inscricao persistentInscricao = em.find(Inscricao.class, inscricao.getId());
            Concurso concursoOld = persistentInscricao.getConcurso();
            Concurso concursoNew = inscricao.getConcurso();
            Candidato candidatoOld = persistentInscricao.getCandidato();
            Candidato candidatoNew = inscricao.getCandidato();
            if (concursoNew != null) {
                concursoNew = em.getReference(concursoNew.getClass(), concursoNew.getId());
                inscricao.setConcurso(concursoNew);
            }
            if (candidatoNew != null) {
                candidatoNew = em.getReference(candidatoNew.getClass(), candidatoNew.getId());
                inscricao.setCandidato(candidatoNew);
            }
            inscricao = em.merge(inscricao);
            if (concursoOld != null && !concursoOld.equals(concursoNew)) {
                concursoOld.getInscricoes().remove(inscricao);
                concursoOld = em.merge(concursoOld);
            }
            if (concursoNew != null && !concursoNew.equals(concursoOld)) {
                concursoNew.getInscricoes().add(inscricao);
                concursoNew = em.merge(concursoNew);
            }
            if (candidatoOld != null && !candidatoOld.equals(candidatoNew)) {
                candidatoOld.setInscricao(null);
                candidatoOld = em.merge(candidatoOld);
            }
            if (candidatoNew != null && !candidatoNew.equals(candidatoOld)) {
                Inscricao oldInscricaoOfCandidato = candidatoNew.getInscricao();
                if (oldInscricaoOfCandidato != null) {
                    oldInscricaoOfCandidato.setCandidato(null);
                    oldInscricaoOfCandidato = em.merge(oldInscricaoOfCandidato);
                }
                candidatoNew.setInscricao(inscricao);
                candidatoNew = em.merge(candidatoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                long id = inscricao.getId();
                if (findInscricao(id) == null) {
                    throw new NonexistentEntityException("The inscricao with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Inscricao inscricao;
            try {
                inscricao = em.getReference(Inscricao.class, id);
                inscricao.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The inscricao with id " + id + " no longer exists.", enfe);
            }
            Concurso concurso = inscricao.getConcurso();
            if (concurso != null) {
                concurso.getInscricoes().remove(inscricao);
                concurso = em.merge(concurso);
            }
            Candidato candidato = inscricao.getCandidato();
            if (candidato != null) {
                candidato.setInscricao(null);
                candidato = em.merge(candidato);
            }
            em.remove(inscricao);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Inscricao> findInscricaoEntities() {
        return findInscricaoEntities(true, -1, -1);
    }

    public List<Inscricao> findInscricaoEntities(int maxResults, int firstResult) {
        return findInscricaoEntities(false, maxResults, firstResult);
    }

    private List<Inscricao> findInscricaoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Inscricao as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Inscricao findInscricao(long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Inscricao.class, id);
        } finally {
            em.close();
        }
    }

    public int getInscricaoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Inscricao as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
